package org.renwei.ml.knn;

import com.google.common.base.Charsets;
import com.google.common.base.Preconditions;
import com.google.common.io.Files;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 手写输入验证
 * Created by renwei on 16/8/19.
 */
public class HandWritingTest {

    //将32x32像素的图像转换为1x1024的一维数组
    private double[] img2vector(String filename) {
        double[] vector = new double[1024];

        File file = new File(filename);
        List<String> flist = null;
        try {
            flist = Files.readLines(file, Charsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int i = 0;
        for (String line : flist) {
            for (int j = 0; j < line.length(); j++) {
                vector[i++] = Double.parseDouble(String.valueOf(line.charAt(j)));
            }

        }
        return vector;
    }

    public void dataReduce(String trainingfPath, String testfPath) {
        long time = System.currentTimeMillis();

        File trainingFile = new File(trainingfPath);
        File testFile = new File(testfPath);
        Preconditions.checkArgument(trainingFile.isDirectory(), "traingfPath is not Directory");
        Preconditions.checkArgument(testFile.isDirectory(), "testFile is not Directory");

        String[] trainingfileList = trainingFile.list();
        String[] testfileList = testFile.list();

        //K值
        int K = 3;

        //训练数据
        int dimension = 1024;       //维度
        double[][] dataSet = new double[trainingfileList.length][1024];
        int[] labels = new int[trainingfileList.length];
        int i = 0;
        for (String fileName : trainingfileList) {
            dataSet[i] = img2vector(trainingfPath + "/" + fileName);

            String digit = fileName.split("_")[0];
            labels[i] = Integer.parseInt(digit);
            i++;
        }

        //测试数据
        double[][] inXArr = new double[testfileList.length][1024];
        int[] testLabels = new int[testfileList.length];
        i = 0;
        for (String fileName : testfileList) {
            inXArr[i] = img2vector(testfPath + "/" + fileName);

            String digit = fileName.split("_")[0];
            testLabels[i] = Integer.parseInt(digit);

            i++;
        }


        //        KNN.kNN(dimension, dataSet, labels, K, inXArr[testfileList.length - 1]);

        //计算结果数据
        //统计结果，错误率
        double count = 0f;
        int[] reduceLabels = new int[testfileList.length];
        for (i = 0; i < testfileList.length; i++) {
            reduceLabels[i] = Integer.parseInt(KNN.kNN(dimension, dataSet, labels, K, inXArr[i]));
            System.out.println(
                i + 1 + ": testLabel:" + testLabels[i] + ", reduceLabel:" + reduceLabels[i]);
            if (testLabels[i] != reduceLabels[i]) {
                count++;
            }
        }


        System.out.printf("the total number of errors is: %f, the total eror rate is %f\n", count,
            count / testfileList.length);

        System.out.printf("total costs %d ms\n", System.currentTimeMillis() - time);

    }


}
